<?php
namespace app\app\controller;
use think\Db;
class Oneself extends Common
{
    /**
     * 获取个人信息
     */
    public function get_info()
    {
        $token = request()->request('token');
        $uid = token_decode($token);
        $table = request()->request('type')?request()->request('type'):'user';
        $msg['state'] = 0;
        $msg['data'] = '';
        if (empty($uid)) {
            $msg['msg'] = '参数错误';
        } else {
            $fileds = 'headimg,name,phone';
            $msg['data'] = Db::table($table)->field($fileds)->where('id', $uid)->find();
            $msg['state'] = 1;
            $msg['msg'] = '获取成功';
        }
        return json($msg);
    }

    /**
     * 修改个人信息(系统设置)
     */
    public function set_info()
    {
        $token = request()->request('token');
        $uid = token_decode($token);
        $table = request()->request('type')?request()->request('type'):'user';
        $msg['state'] = 0;
        $msg['data'] = '';
        if ($_FILES){
            $headimg = upload();
        }else{
            $headimg = '';
        }
        $name = request()->request('name');
        if (empty($uid)) {
            $msg['msg'] = '参数错误';
        } else {
          if(!empty($name)){
                $data['name'] = $name;
           }
          if(!empty($headimg)){
                $data['headimg'] = $headimg['headimg']['url'];
            }
          Db::table($table)->where('id', $uid)->update($data);
          $msg['state'] = 1;
          $msg['msg'] = '修改成功';
       }
        return json($msg);
    }

    /**
     * 获取用户的地址  oneself/get_address
     */
    public function get_address()
   {
        $token = request()->request('token');
        $uid = token_decode($token);
        $msg['state'] = 0;
        $msg['data'] = [];
        if (empty($uid)) {
            $msg['msg'] = '参数错误';
        } else {
            $msg['data'] = Db::table('address')->query("select id as address_id,address,area,address_detail,phone,`name`,lng,lat,def from `address` where uid='" . $uid . "' order by def desc");
            $msg['state'] = 1;
            $msg['msg'] = '获取成功';
        }
        return json($msg);
    }

    /**
     * 设置默认地址
     */
    public function set_default_address()
    {
        $token = request()->request('token');
        $uid = token_decode($token);
        $address_id = request()->request('address_id');
        $msg['state'] = 0;
        $msg['data'] = '';
        if (empty($uid)) {
            $msg['msg'] = '参数错误';
        } else {
            $msg['msg'] = '操作成功';
            $msg['state'] = 1;
            Db::table('address')->where('uid', $uid)->update(['def' => 0]);
            Db::table('address')->where(['uid' => $uid, 'id' => $address_id])->update(['def' => 1]);
        }
        return json($msg);
    }

    /**
     * 添加、编辑地址
     */
    public function add_address()
    {
        $token = request()->request('token');
        $uid = token_decode($token);
        $address_id = request()->request('address_id');
        $msg['state'] = 0;
        $msg['data'] = '';
        $data = [];
        if (empty($uid)) {
            $msg['msg'] = '参数错误';
        } else {
            $data['uid'] = $uid;
            $data['name'] = request()->request('name');
            $data['phone'] = request()->request('phone');
            $data['lng'] = request()->request('lng');
            $data['lat'] = request()->request('lat');
            $data['area']= request()->request('area');//所在地区
            $data['address'] = request()->request('address');
            $data['address_detail'] = request()->request('address_detail');
            if($address_id == ''){
                if(empty($data['lng']) || empty($data['lat']) || empty($data['phone'])){
                    $msg['msg'] = '参数错误';
                }else{
                    $count = Db::table('address')->where('uid', $uid)->count();
                    if ($count == 0) {
                        $data['def'] = 1;
                    }
                    Db::table('address')->insert($data);
                }
            }else{
                foreach ($data as $k => $v){
                    if($v == '')
                        unset($data[$k]);
                }
                Db::table('address')->where(['uid'=>$uid,'id'=>$address_id])->update($data);
            }
            $msg['state'] = 1;
            $msg['msg'] = '添加成功';
        }
        return json($msg);
    }

    /**
     * 删除地址
     */
    public function del_address()
    {
        $token = request()->request('token');
        $uid = token_decode($token);
        $address_id = request()->request('address_id');
        $msg['state'] = 0;
        $msg['data'] = '';
        if (empty($uid) || empty($address_id)) {
            $msg['msg'] = '参数错误';
        } else {
            $msg['state'] = 1;
            $check = Db::table('address')->where(['uid'=>$uid,'id'=>$address_id])->find();
            if($check['def'] == 1){
                $new = Db::table('address')->where(['uid'=>$uid])->where('id!='.$check['id'])->find();
                Db::table('address')->where(['uid'=>$uid,'id'=>$new['id']])->update(['def'=>1]);
            }
            Db::table('address')->where(['uid'=>$uid,'id'=>$address_id])->delete();
            $msg['msg'] = '操作成功';
        }
        return json($msg);
    }

//    /**
//     * 关于我们  oneself/get_about
//     */
//    public function get_about()
//    {
////        $content = '<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />';
////        return $content.Db::table('notice')->where('title','关于我们')->value('content');
//        $msg['state'] = 1;
//        $msg['data'] = Db::table('notice')->where('title','关于我们')->value('content');
//        $msg['msg'] = '获取成功';
//        return json($msg);
//    }
   /***关于咱县**/
    public function get_about_mine(){
     $content = '<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />';
     return $content.Db::table('notice')->where('title','关于咱县')->value('content');
    }
    /***司机使用帮助****/
    public function get_driver_help(){
     $content = '<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />';
     return $content.Db::table('notice')->where('title','司机使用帮助')->value('content');   
    }

//    /**
//     * 意见反馈  oneself/send_opinion
//     */
//    public function send_opinion()
//    {
//        $token = request()->request('token');
//        $uid = token_decode($token);
//        $content = request()->request('content');
//        $msg['state'] = 0;
//        $msg['data'] = '';
//        if (empty($uid)) {
//            $msg['msg'] = '参数错误';
//        }else{
//            $area = Db::table('user')->field('pid,cid,aid')->where('id',$uid)->find();
//            $area['uid'] = $uid;
//            $area['content'] = $content;
//            $area['time'] = time();
//            Db::table('opinion')->insert($area);
//            $msg['state'] = 1;
//            $msg['msg'] = '操作成功';
//        }
//        return json($msg);
//    }
    //意见反馈
    public function send_new_opinion(){
      $token=request()->request("token");  
      $uid=token_decode($token);
      $content=request()->request("content");
      $msg['state']=0;
      $msg['data']='';
      if(empty($token)||empty($uid)){
        $msg['msg']="参数错误"; 
      }else{
          $area=Db::table("user")->field('pid,cid,aid')->where('id',$uid)->find();
          $area['uid']=$uid;
          $area['content']=$content;
          $area['time']=time();
          Db::table("opinion")->insert($area);
          $msg['state']=1;
          $msg['msg']="success";
      }
      return json($msg);  
    }
    
    
    

//    /**
//     * 检查更新
//     */
//    public function check_update()
//    {
//        $this_version = request()->request('version');
//        $type = request()->request('type')?request()->request('type'):'user';
//        $msg['state'] = 0;
//        $msg['data'] = null;
//        $update_config = Db::table('update')->field('url,size,version,description,constraint')->where('type',$type)->order('time desc')->find();
//        if(count($update_config) == 0){
//            $msg['msg'] = '当前已是最新版本';
//        }else{
//            if(str_replace('.','',$this_version) < str_replace('.','',$update_config['version'])){
//                $msg['state'] = 1;
//                $msg['msg'] = '检测到新版本，请更新';
//                $msg['data'] = $update_config;
//            }else{
//                $msg['msg'] = '当前已是最新版本';
//            }
//        }
//        return json($msg);
//    }
    /***检查版本更新***/
    public function check_update(){
     $old_version=request()->request("version");   
     $type=request()->request("type")?request()->request("type"):"user";   
     $update_config=Db::table("update")->field("url,size,version,description,constraint")->where('type',$type)->order('time desc')->find();
     if(count($update_config)==0){
        $msg['state']=0;   
        $msg['data']=(object)[];
        $msg['msg']="当前已经是最新版本";  
      }else{
          if(str_replace('.','',$update_config['version'] > str_replace('.','',$old_version))){
              $msg['state']=1;   
              $msg['msg']="有新版本，请更新版本";
              $msg['data']=$update_config;
          }else{
               $msg['state']=0;   
               $msg['data']=(object)[];
               $msg['msg']="当前已是最新版本";
          }
      } 
      return json($msg);
    }
    
//    //完成订单
//    public function complete_order(){
//        $order_id=request()->request("order_id");//订单唯一id
//        if(empty($order_id)){
//         $msg['state']=0;   
//         $msg['data']=(object)[];
//         $msg['msg']="订单id消失了~~~";   
//        }else{
//          $order=Db::table("order")->where(["order_id"=>$order_id])->find();  
//          $uid=$order['express'];
//          if(count($order)>0 && $order['status']==9){
//            $msg['state']=1;
//            Db::table("order")->where(["express"=>$uid,"order_id"=>$order_id])->update(["ident"=>"",'complete_time'=>time()]);
//            Db::table("earning")->insert(["order_id"=>$order_id,'money'=>$order['express_get_money'],'time'=>time(),"type"=>"express"]);  //插入表中字段
//            Db::table("user")->where("id",$uid)->setInc(["money"=>$order['express_get_money']]);//步长自增长
//            //推送服务
//            push(md5($order['uid']),json_encode(['type'=>1,'message'=>'订单完成~~~','parameters'=>$order_id,'status'=>5]));
//            insert_message($order['uid'],'订单提醒','您的订单已完成',5,$order_id);
//            $msg['msg']="success";
//           }else{   
//           $msg['msg']='非法操作';
//           }  
//            
//         }
//       return json($msg);
//    }
    
    /**
     * 修改密码
     */
    public function edit_pwd()
    {
        $token = request()->request('token');
        $uid = token_decode($token);
        $old_pwd = md5(request()->request('old_pwd'));
        $new_pwd = md5(request()->request('new_pwd'));
        $table = request()->request('type')?request()->request('type'):'user';
        $msg['state'] = 0;
        $msg['data'] = '';
        if (empty($uid) || empty($old_pwd) || empty($new_pwd)) {
            $msg['msg'] = '参数错误';
        } else {
            $user = Db::table($table)->where('id',$uid)->find();
            if($user['password'] != $old_pwd){
                $msg['msg'] = '原密码输入错误';
            }else{
                $msg['state'] = 1;
                Db::table($table)->where('id',$uid)->update(['password'=>$new_pwd]);
                $msg['msg'] = '修改成功';
            }
        }
        return json($msg);
    }

    //获取消息列表
    public function get_message_list()
    {
        $token = request()->request('token');
        $uid = token_decode($token);
        $msg['state'] = 1;
        $page = request()->request('page')?request()->request('page'):1;
        $pagesize = request()->request('pagesize')?request()->request('pagesize'):10;
        $fields = 'title,message,type,parameters,time';
        $msg['data'] = Db::table('message')->field($fields)->where('uid',$uid)->page($page,$pagesize)->order('time desc')->select();
        $msg['msg'] = '获取成功';
        return json($msg);
    }

}